package com.example.qrcode.tool;
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.Protocol;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.Bucket;

import java.util.List;

/**
 * OSS SDK 基础使用示例
 * 展示如何初始化 OSS 客户端并列出所有 Bucket
 */
public class Demo {
    public static void main(String[] args) throws Exception {
        // 创建 ClientBuilderConfiguration 实例，用于配置 OSS 客户端参数
        ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
        clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
        clientBuilderConfiguration.setProtocol(Protocol.HTTPS);

        // 直接在代码中配置访问凭证（替换为你的实际AccessKey）
        String accessKeyId = "LTAI5tM2BfWHzFPYPB86qio1";
        String accessKeySecret = "MVYcQsyUbcXA1OJ5d9AQOERsbwoxek";
        // 创建固定凭证提供者（无需依赖环境变量）
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);

        // 创建 OSS 客户端实例
        OSS ossClient = OSSClientBuilder.create()
                .endpoint("oss-cn-hangzhou.aliyuncs.com") // 你的Endpoint
                .credentialsProvider(credentialsProvider) // 使用代码中配置的凭证
                .clientConfiguration(clientBuilderConfiguration)
                .region("cn-hangzhou") // 你的Region
                .build();

        try {
            // 列出当前用户的所有 Bucket
            List<Bucket> buckets = ossClient.listBuckets();
            for (Bucket bucket : buckets) {
                System.out.println(bucket.getName());
            }
        } finally {
            ossClient.shutdown();
        }
    }
}